/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package QLThuVien.DAL;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;

/**
 *
 * @author Rado
 */
public class MyConnectUnit {
   //private MySQLConnectData connect;
    private MSSQLConnectData connect;
    
    public ResultSet Select(String TableName, String Condition, String OrderBy) throws Exception{
        StringBuilder query = new StringBuilder(" SELECT * FROM " + TableName);
        
        this.AddCondition(query, Condition);
        
        this.AddOrderBy(query, OrderBy);
        
        query.append(";");
        
        return this.connect.excuteQuery(query.toString());
    }
    
    public ResultSet Select(String TableName, String Condition) throws Exception{
        StringBuilder query = new StringBuilder(" SELECT * FROM " + TableName);
        
        this.AddCondition(query, Condition);
        
        query.append(";");
        
        return this.connect.excuteQuery(query.toString());
    }
    
    public ResultSet Select(String TableName) throws Exception{
        StringBuilder query = new StringBuilder(" SELECT * FROM " + TableName);
        
        query.append(";");
        
        return this.connect.excuteQuery(query.toString());
    }
    
    private void AddCondition(StringBuilder query, String Condition){
        if(Condition != null)
            query.append(" WHERE " + Condition);
    }
    
    private void AddOrderBy(StringBuilder query, String OrderBy){
        if(OrderBy != null)
            query.append(" ORDER BY " + OrderBy);
    }
    
    public boolean Insert(String TableName, HashMap<String,Object> ColumnValues) throws Exception{
        StringBuilder query = new StringBuilder("INSERT INTO " + TableName);
        
        StringBuilder valueInsert = new StringBuilder();
        
        query.append("(");
        
        for(String key : ColumnValues.keySet()){
            query.append(key+",");
            valueInsert.append("'"+ColumnValues.get(key).toString() + "' ,");
        }
        
        query = query.delete(query.length() - 1, query.length());
        valueInsert = valueInsert.delete(valueInsert.length() - 1, valueInsert.length());
        
        query.append(") VALUES(" + valueInsert.toString()+")");
        
        query.append(";");
        
        return this.connect.executeUpdate(query.toString()) > 0;
    }
    
    public boolean Update(String TableName, HashMap<String,Object> ColumnValues, String Condition) throws Exception{
        StringBuilder query = new StringBuilder(" UPDATE " + TableName + " SET ");
        
        for(String key : ColumnValues.keySet()){
            query.append(key + " = '"+ColumnValues.get(key).toString() + "' ,"); 
        }
        
        query = query.delete(query.length() - 1, query.length());
        
        this.AddCondition(query, Condition);
        
        query.append(";");
        
        return this.connect.executeUpdate(query.toString()) > 0;
    }
    
    public boolean Delete(String TableName, String Condition) throws Exception{
        StringBuilder query = new StringBuilder(" DELETE FROM " + TableName);
        
        this.AddCondition(query, Condition);
        
        query.append(";");
        
        return this.connect.executeUpdate(query.toString()) > 0;
    }
    
    public static int getColumnCount(ResultSet result) throws SQLException{
        return result.getMetaData().getColumnCount();
    }
    
    public static String[] getColumnName(ResultSet result) throws SQLException{
        ResultSetMetaData rsMetaData = (ResultSetMetaData) result.getMetaData();
        
        int ColumnCount = rsMetaData.getColumnCount();
        
        String[] list = new String[ColumnCount];
        
        for(int i = 0; i < ColumnCount; i++)
            list[i] = rsMetaData.getColumnName(i);
        return list;
    }
    
    public void Close() throws SQLException{
        connect.Close();
    }
    
    public MyConnectUnit(String host, String user, String pass, String database){
        connect = new MSSQLConnectData(host, user, pass, database);
    }
}
